package org.jeecg.modules.complain.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.util.CommonDefine;
import org.jeecg.modules.complain.entity.Repair;
import org.jeecg.modules.complain.entity.RepairStatus;
import org.jeecg.modules.complain.mapper.PrjBtnMapper;
import org.jeecg.modules.complain.mapper.RepairConfigMapper;
import org.jeecg.modules.complain.mapper.RepairMapper;
import org.jeecg.modules.complain.mapper.RepairStatusMapper;
import org.jeecg.modules.complain.service.IRepairStatusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Description: 报修状态
 * @Author: jeecg-boot
 * @Date:   2019-08-05
 * @Version: V1.0
 */
@Service
public class RepairStatusServiceImpl extends ServiceImpl<RepairStatusMapper, RepairStatus> implements IRepairStatusService {

    @Autowired
    private RepairStatusMapper repairStatusMapper;
    @Autowired
    private RepairConfigMapper repairConfigMapper;
    @Autowired
    private PrjBtnMapper prjBtnMapper;
    @Autowired
    private RepairMapper repairMapper;

    @Override
    public RepairStatus selectByRepairId(String depairId,Integer status) {
        return repairStatusMapper.selectByRepairId(depairId,status);
    }


    @Override
    public List<Repair> selectByUserId(String userId, Integer status,String officeId,List<String> reciveIdList,List<String>positionIdList) {
        if (status == CommonDefine.REPAIR_STATUS_300){
            return repairStatusMapper.findHangUp(userId,status,CommonDefine.REPAIR_STATUS_500);
        }else if (status == CommonDefine.REPAIR_STATUS_200){
            boolean flag =false;
            List<Repair> repairs1 = repairStatusMapper.selectAcpet(CommonDefine.REPAIR_STATUS_100,officeId);
            List<Repair> repairs = repairStatusMapper.selectByUserId(userId, status,officeId);
            for (String recive:reciveIdList) {
                 for (String position:positionIdList) {
                      if (recive.equals(position)){
                           flag = true;
                           repairs.addAll(repairs1);
                           break ;
                      }
                 }
                      if (flag){
                              break;
                      }
            }
             return repairs;
        }else if (status == CommonDefine.REPAIR_STATUS_600){
            List<Repair> repairs1 = repairStatusMapper.selectByUserId(userId, CommonDefine.REPAIR_STATUS_600,officeId);
            List<Repair> repairs = repairStatusMapper.selectByUserId(userId, CommonDefine.REPAIR_STATUS_900,officeId);
            repairs.addAll(repairs1);
            Collections.sort(repairs, new Comparator<Repair>(){
                public int compare(Repair o1, Repair o2) {
                    if(after(o1.getRepairStatus().getCreateTime(), o2.getRepairStatus().getCreateTime())){
                        return 1;
                    }
                 return 1;
                }
            });

            return repairs;
        }else {
            return repairStatusMapper.selectByUserId(userId, status,officeId);
        }
    }

    public static boolean after(Date date1,Date date2){
        Calendar c1 = Calendar.getInstance();
        c1.setTime(date1);
        Calendar c2 = Calendar.getInstance();
        c2.setTime(date2);
        return c2.after(c1);
    }

    @Override
    public Integer delete(String repairId,Integer status) {
        return repairStatusMapper.deleteByrepairId(repairId,status);
    }

    @Override
    public List<RepairStatus> selectRepairStatus(String repairId) {
        return repairStatusMapper.selectRepairStatus(repairId);
    }


    @Override
    public Integer returnRepair(RepairStatus repairStatus) {
         Integer integer = repairMapper.updateRepairStatus(repairStatus.getRepairId(), CommonDefine.REPAIR_STATUS_100);
         repairStatusMapper.deleteByrepairId(repairStatus.getRepairId(),CommonDefine.REPAIR_STATUS_200);
         repairStatus.setStatus(CommonDefine.REPAIR_STATUS_100);
         repairStatus.setCreateTime(new Date());
         int insert = repairStatusMapper.insert(repairStatus);
         return insert;
       }

    @Override
    public Integer statisticsRepair(String userId, Integer status) {
        Integer count = repairStatusMapper.statisticsRepair(userId, status);
        if (count == null) return 0;
        return count;
    }
}
